/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/util/CollectionUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: CollectionUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author elancom
 * @date Apr 28, 2012
 */
public class CollectionUtils {
	public static <T> List<T> createEmptyList() {
		return new ArrayList<T>();
	}

	public static <T> T get(Collection<T> collection, int index) {
		if (isEmpty(collection)) {
			return null;
		}

		if (index < 0) {
			index = 0;
		}
		if (index + 1 > collection.size()) {
			index = collection.size() - 1;
		}

		if (collection instanceof List<?>) {
			return ((List<T>) collection).get(0);
		}

		return null;
	}

	public static boolean isEmpty(Collection<?> collection) {
		return collection == null ? true : collection.isEmpty();
	}

	public static String join(Collection<?> collection) {
		return join(collection, ",");
	}

	public static String join(Collection<?> collection, String joinCh) {
		if (isEmpty(collection)) {
			return "";
		}

		final Object[] objs = collection.toArray();
		final StringBuffer sb = new StringBuffer();

		for (final Object obj : objs) {
			sb.append(obj.toString()).append(joinCh);
		}

		if (sb.length() > 0) {
			sb.delete(sb.length() - joinCh.length(), sb.length());
		}

		return sb.toString();
	}

	public static <T> Set<T> toSet(List<T> list, Set<T> set) {
		if (list == null || list.isEmpty()) {
			return set;
		}

		if (set == null) {
			set = new HashSet<T>();
		}

		set.addAll(list);
		return set;
	}

	public static <K, V> List<V> toList(Map<K, V> map, List<V> list) {
		if (map == null) {
			return null;
		}

		if (list == null) {
			list = new ArrayList<V>();
		}

		final List<V> _list = list;
		JoyfulUtils.iterEach(map, new IterEach<K, V>() {
			@Override
			public boolean each(K key, V value) {
				_list.add(value);
				return true;
			}
		});

		return list;
	}

	public static <K, V> List<V> toList(Set<V> set, List<V> list) {
		if (set == null) {
			return null;
		}

		if (list == null) {
			list = new ArrayList<V>();
		}

		final List<V> _list = list;
		JoyfulUtils.forEach(set, new ForEach<V>() {
			@Override
			public boolean each(int index, V value) {
				_list.add(value);
				return true;
			}
		});
		return _list;
	}

	public static <T> void addArray(final Collection<T> coll, Object[] objs) {
		if (coll == null || objs == null || objs.length == 0) {
			return;
		}

		JoyfulUtils.forEach(objs, new ForEach<Object>() {
			@SuppressWarnings("unchecked")
			@Override
			public boolean each(int index, Object value) {
				coll.add((T) value);
				return true;
			}
		});
	}

	private CollectionUtils() {
	}
}
